<?php
namespace app\task\logic;

class CpFormBranchMonthLogic extends BaseLogic{

    /**
     * 获取需要计算的时间
     * @param $branchid
     * @param $now_time
     * @return mixed
     */
    public function get_count_time($branchid,$now_time){
        $maps = [];
        $maps[] = ['branchid','=',$branchid];
        $cfbm_detail = model('cp_form_branch_month')->get_detail($maps,'id,timeid','timeid desc');
        if(!empty($cfbm_detail)){
            $timeid = $cfbm_detail['timeid'];

            $date_1 = date('Y-m-01',strtotime($timeid));
            $date_2 = date('Y-m-d H:i:s',strtotime($date_1));

            //获取指定日期所在月的开始时间和结束时间
            $r = get_the_month_time($date_2);

            if($now_time >= $r['start_time'] && $now_time <= $r['end_time']){
                $start_time = $r['start_time'];
                $end_time = $r['end_time'];
            }else{
                $date_2 = date('Y-m-d H:i:s',strtotime('+1 month',strtotime($date_1)));
                $r = get_the_month_time($date_2);
                $start_time = $r['start_time'];
                $end_time = $r['end_time'];
            }
        }else{
            $start_time = '2019-10-01 00:00:00';
            $end_time = '2019-10-31 23:59:59';
        }

        $data['start_time'] = $start_time;
        $data['end_time'] = $end_time;
        $data['timeid'] = date('Y-m',strtotime($start_time));
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 计算分部社区已巡店次数
     * @param $branchid
     * @param $start_time
     * @param $end_time
     * @return mixed
     */
    public function count_cp_branch_num($branchid,$start_time,$end_time){
        $start_date = date('Y-m-d',strtotime($start_time));
        $end_date = date('Y-m-d',strtotime($end_time));

        $maps = [];
        $maps[] = ['branchid','=',$branchid];
        $maps[] = ['time_code','<>','other'];
        $maps[] = ['now_date','between',[$start_date,$end_date]];
        $maps[] = ['holiday','=',0];
        $maps[] = ['status','=',1];

        $num = model('cp_branch')->get_count($maps,'id');
        return $num;
    }

    /**
     * 计算分部社区未巡店次数
     * @param $branchid
     * @param $start_time
     * @param $end_time
     * @return mixed
     */
    public function count_cp_branch_no_num($branchid,$start_time,$end_time){
        $now_date = date('Y-m-d');
        $start_date = date('Y-m-d',strtotime($start_time));
        $end_date = date('Y-m-d',strtotime($end_time));

        $maps = [];
        $maps[] = ['branchid','=',$branchid];
        $maps[] = ['time_code','<>','other'];
        $maps[] = ['now_date','between',[$start_date,$end_date]];
        $maps[] = ['now_date','<',$now_date];
        $maps[] = ['holiday','=',0];
        $maps[] = ['status','=',0];

        $num = model('cp_branch')->get_count($maps,'id');
        return $num;
    }
}